/*
 * MIDPFont.java
 *
 * Created on 2005/02/20 +0900
 *
 * $Revision: 42 $
 * $Date: 2008-10-26 14:12:53 +0000 (Sun, 26 Oct 2008) $
 */

package org.gikolet.framework.platform.midp.internal;

import org.gikolet.framework.ui.AbstractFont;
import org.gikolet.framework.ui.Font;
import org.gikolet.framework.ui.FontProvider;

/**
 * @author Tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 42 $ $Date: 2008-10-26 14:12:53 +0000 (Sun, 26 Oct 2008) $
 */
public class MIDPFont extends AbstractFont {
    private javax.microedition.lcdui.Font midpFont;

    public MIDPFont(FontProvider fontProvider, javax.microedition.lcdui.Font midpFont) {
	super(fontProvider, getFace(midpFont), getStyle(midpFont), getSize(midpFont));

	this.midpFont = midpFont;
    }

    MIDPFont(FontProvider fontProvider, int face, int style, int size) {
	super(fontProvider, face, style, size);

	int midpFace;
	int midpStyle;
	int midpSize;

	switch (face) {
	    case Font.FACE_MONOSPACE:
		midpFace = javax.microedition.lcdui.Font.FACE_MONOSPACE;
		break;
	    case Font.FACE_PROPORTIONAL:
		midpFace = javax.microedition.lcdui.Font.FACE_PROPORTIONAL;
		break;
	    case Font.FACE_SYSTEM:
		midpFace = javax.microedition.lcdui.Font.FACE_SYSTEM;
		break;
	    default:
		throw new IllegalArgumentException("Invalid face(MIDP) is " + face);
	}

	midpStyle = 0;
	if ((style & Font.STYLE_PLAIN) != 0) {
	    if ((style & Font.STYLE_BOLD) != 0 || (style & Font.STYLE_ITALIC) != 0) {
		throw new IllegalArgumentException("Invalid style(MIDP) is " + style);
	    }
	    style -= Font.STYLE_PLAIN;
	    midpStyle |= javax.microedition.lcdui.Font.STYLE_PLAIN;
	} else {
	    if ((style & Font.STYLE_BOLD) != 0) {
		style -= Font.STYLE_BOLD;
		midpStyle |= javax.microedition.lcdui.Font.STYLE_BOLD;
	    }
	    if ((style & Font.STYLE_ITALIC) != 0) {
		style -= Font.STYLE_ITALIC;
		midpStyle |= javax.microedition.lcdui.Font.STYLE_ITALIC;
	    }
	}
	if ((style & Font.STYLE_UNDERLINED) != 0) {
	    style -= Font.STYLE_UNDERLINED;
	    midpStyle |= javax.microedition.lcdui.Font.STYLE_UNDERLINED;
	}
	if (style != 0) {
	    throw new IllegalArgumentException("Invalid style(MIDP) is " + style);
	}

	switch (size) {
	    case Font.SIZE_LARGE:
		midpSize = javax.microedition.lcdui.Font.SIZE_LARGE;
		break;
	    case Font.SIZE_MEDIUM:
		midpSize = javax.microedition.lcdui.Font.SIZE_MEDIUM;
		break;
	    case Font.SIZE_SMALL:
	    case Font.SIZE_TINY:
		midpSize = javax.microedition.lcdui.Font.SIZE_SMALL;
		break;
	    default:
		throw new IllegalArgumentException("Invalid size(MIDP) is " + size);
	}

	this.midpFont = javax.microedition.lcdui.Font.getFont(midpFace, midpStyle,
		midpSize);
    }

    private static int getStyle(javax.microedition.lcdui.Font midpFont) {
	int style = 0;

	if (midpFont.isPlain()) {
	    style |= Font.STYLE_PLAIN;
	} else {
	    if (midpFont.isBold()) {
		style |= Font.STYLE_BOLD;
	    }
	    if (midpFont.isItalic()) {
		style |= Font.STYLE_ITALIC;
	    }
	}
	if (midpFont.isUnderlined()) {
	    style |= Font.STYLE_UNDERLINED;
	}

	return style;
    }

    private static int getSize(javax.microedition.lcdui.Font midpFont) {
	int size = 0;

	if (midpFont.getSize() == javax.microedition.lcdui.Font.SIZE_LARGE) {
	    size = Font.SIZE_LARGE;
	} else if (midpFont.getSize() == javax.microedition.lcdui.Font.SIZE_MEDIUM) {
	    size = Font.SIZE_MEDIUM;
	} else if (midpFont.getSize() == javax.microedition.lcdui.Font.SIZE_SMALL) {
	    size = Font.SIZE_SMALL;
	}

	return size;
    }

    private static int getFace(javax.microedition.lcdui.Font midpFont) {
	int face = 0;

	if (midpFont.getFace() == javax.microedition.lcdui.Font.FACE_MONOSPACE) {
	    face = Font.FACE_MONOSPACE;
	} else if (midpFont.getFace() == javax.microedition.lcdui.Font.FACE_PROPORTIONAL) {
	    face = Font.FACE_PROPORTIONAL;
	} else if (midpFont.getFace() == javax.microedition.lcdui.Font.FACE_SYSTEM) {
	    face = Font.FACE_SYSTEM;
	}

	return face;
    }

    public javax.microedition.lcdui.Font getMIDPFont() {
	return this.midpFont;
    }

    public int getBaselinePosition() {
	return this.midpFont.getBaselinePosition();
    }

    public int getHeight() {
	return this.midpFont.getHeight();
    }

    public int stringWidth(String str) {
	return this.midpFont.stringWidth(str);
    }

    public int charWidth(char c) {
	return this.midpFont.charWidth(c);
    }

    public int substringWidth(String str, int offset, int lenght) {
	return this.midpFont.substringWidth(str, offset, lenght);
    }
}
